package com.lei.service.impl;

import com.lei.dao.RoleDao;
import com.lei.dao.impl.RoleDaoImpl;
import com.lei.entity.Role;
import com.lei.service.RoleService;
import com.lei.util.PageBean;

import java.util.List;

/**
 * @author Lei Chu
 * @Date 2020/10/17 19:46
 */
public class RoleServiceImpl implements RoleService {

    private final RoleDao roleDao = new RoleDaoImpl();

    @Override
    public List<Role> getAllRole() {
        return roleDao.getAllRole();
    }

    @Override
    public PageBean getAllRoleByPage(Role role) {
        PageBean<Role> page = new PageBean<>();
        //获得总条数
        int count = roleDao.getCount(role);
        if (count <= 0) {
            //条数 < 0 将空page返回
            return page;
        }
        page.setCount(Long.parseLong(count + ""));
        //获得职位，装填到page中
        List<Role> list = roleDao.getAllRoleByPage(role);
        page.setData(list);
        // 查到数据返回装填之后的page
        return page;
    }

    @Override
    public int addRole(Role role) {
        if (role.getRid() != null) {
            return roleDao.updateRole(role);
        }
        return roleDao.addRole(role);
    }

    @Override
    public String checkAddName(Integer rid, String rname) {
        Integer result = roleDao.checkAddName(rname);
        if (result != null && !result.equals(rid)){
            //根据名字找到了ID，并且 找到的ID和原来的ID不相等，说明该名字不可用 返回 0
            return "0";
        }
        //根据名字没有找到ID 返回 1 表示这个名字可用
        return "1";
    }

    @Override
    public int deleteRole(int rid) {
        return roleDao.deleteRole(rid);
    }
}
